<%--
  - Copyright 2011 Angel Sanadinov
  -
  - This file is part of VBox WMI.
  -
  - VBox WMI is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - VBox WMI is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
  --%>

<%@page import="Utilities.UIFunctions"%>
<%@page import="Utilities.Functions"%>
<%@page import="Beans.Requests.DataRetrieval.UsersDataRetrievalRequest"%>
<%@page import="Utilities.ParameterNames"%>
<%@page import="Utilities.ParameterNames.Service"%>
<%@page import="Utilities.Constants"%>
<%@page import="Utilities.Constants.SendingRequestResult"%>
<%@page import="Beans.Responses.DataRetrievalComponents.UserDataComponent"%>
<%@page import="Beans.Responses.DataRetrievalResponse"%>
<%@page import="Managers.Utilities.User"%>
<%@page contentType="text/html" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@taglib uri="/WEB-INF/tlds/pagePrintingTags.tld" prefix="p" %>
<%
    //retrieves user data from the session
    User userSessionData = (User)session.getAttribute(ParameterNames.SESSION_USER_OBJECT);
    
    //data retrieval request and response objects
    UsersDataRetrievalRequest dataRequest = null;
    DataRetrievalResponse<UserDataComponent> data = null;

    //attempts to retrieve the user id, if supplied
    int viewUserId = Constants.INVALID_USER_ID;
    try
    {
        viewUserId = Integer.parseInt(request.getParameter("id"));
    }
    catch(NumberFormatException e){}

    if(Functions.isUserIdValid(viewUserId)) //a single user is to be retrieved
        dataRequest = new UsersDataRetrievalRequest(userSessionData.getUserData().getUserId(), viewUserId);
    else //all users are to be retrieved
        dataRequest = new UsersDataRetrievalRequest(userSessionData.getUserData().getUserId());

    //attempts to send the request and retrieves the response
    if(UIFunctions.sendServiceRequest(request, response, dataRequest, Service.DATA_RETRIEVAL) == SendingRequestResult.OK)
        data = (DataRetrievalResponse<UserDataComponent>)UIFunctions.retrieveServiceResponse(request, Service.DATA_RETRIEVAL);
    else
        ;
%>
<p:printPageHeader pageName="Users Page" pageId="pageUsers" userSessionData="<%= userSessionData %>"/>
        <p:printUsers data="<%= data %>" userId="<%= userSessionData.getUserData().getUserId() %>"
                      isManager="<%= userSessionData.getUserData().getAccountLevel() >= Constants.DB_USER_ACCOUNT_LEVEL_MANAGER %>"/>
<p:printPageFooter/>
